import {
  throttle
} from '../../utils/util'
import api from '../../utils/api' // 引入api接口管理文件
import customerApi from '../../utils/customerApi' // 引入api接口管理文件
const WxParse = require('../../components/wxParse/wxParse');
const app = getApp();

Page({
  onLoad: function (options) {
    app.globalData.districtInfos = [];
    if (options && options.hasOwnProperty('scene')) {
      // options = options.scene
      let sceneString = decodeURIComponent(options.scene)
      if (typeof (sceneString) === 'string') {
        let sceneArray = sceneString.split('&')
        let newKey = ['type', 'productMirrorId', 'productId']
        sceneArray.forEach(item => {
          newKey.findIndex(keyWord => {
            if (item.includes(keyWord)) {
              options[keyWord] = item.split(`${keyWord}=`)[1]
            }
          })
        })
      }
    }
    let type = options.type || 1
    let productMirrorId = ''
    let productId = ''
    if (type * 1 === 1) {
      productMirrorId = options.productMirrorId || '1825'
    }

    if (type * 1 === 2) {
      productId = options.productId
    }

    options.withCustomer !== 'true' && wx.removeStorageSync('customerUid')

    this.setData({
      productMirrorId: productMirrorId,
      isFullSucreen: app.globalData.isFullSucreen,
      type: type * 1,
      productId: productId
      // options: JSON.stringify(options)
    }, () => {
      this.getList()
    })
    this.videoContext = wx.createVideoContext('myVideo')

  },

  onHide() {
    this.videoContext.stop()
  },

  onShareAppMessage() {
    let path = ''
    if (this.data.type * 1 === 1) {
      path = `pages/detail/detail?type=${this.data.type}&productMirrorId=${this.data.productMirrorId}`
    }
    if (this.data.type * 1 === 2) {
      path = `pages/detail/detail?type=${this.data.type}&productId=${this.data.productId}`
    }
    return {
      title: this.data.detailData.name,
      path: path
    }

  },

  onShareTimeline() {
    let path = ''
    if (this.data.type * 1 === 1) {
      path = `pages/detail/detail?type=${this.data.type}&productMirrorId=${this.data.productMirrorId}`
    }
    if (this.data.type * 1 === 2) {
      path = `pages/detail/detail?type=${this.data.type}&productId=${this.data.productId}`
    }
    return {
      title: this.data.detailData.name,
      path: path,
      query: {
        type: this.data.type,
        productId: this.data.productId || '',
        productMirrorId: this.data.productMirrorId || ''
      }
    }
  },

  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    dialogShow: false,
    productMirrorId: null,
    detailData: {},
    productId: '',
    mirrorInfos: [],
    type: null,
    paramInfosNum: 0,
    isFullSucreen: false,
    showShare: false,
    showWaringMessage: false,
    chooseTypeItem: {},
    needRead: false
  },

  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  moveTo(e) {
    let url = e.currentTarget.dataset.url
    if (url === 'serveArea') {
      let districtInfos = this.data.detailData.orgsInDistricts.districtInfos
      app.globalData.districtInfos = districtInfos
      throttle(function (e) {
        wx.navigateTo({
          url: `../${url}/${url}`
        })
      }, 2000)
    }

    if (url === 'userAssess') {
      const vaccineInfoId = this.data.detailData.vaccineInfoId
      throttle(function (e) {
        wx.navigateTo({
          url: `../${url}/${url}?vaccineInfoId=${vaccineInfoId}`
        })
      }, 2000)
    }
  },

  moveToOrder(chooseType) {
    if (!wx.getStorageSync('token') || !wx.getStorageSync('bindMobile')) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../login/login`
        })
      }, 2000)
      return
    }
    let chooseDetail = {
      ...this.data.detailData,
      chooseType: chooseType.detail
    }
    // console.log(chooseDetail)
    app.globalData.chooseDetail = chooseDetail
    throttle(function () {
      wx.navigateTo({
        url: `../order/order`
      })
    }, 2000)
  },

  orderDialog() {
    if (!wx.getStorageSync('token') || !wx.getStorageSync('bindMobile')) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../login/login`
        })
      }, 2000)
      return
    }
    if (this.data.type === 1) {
      const starTime = new Date().getTime()
      console.log('productVaccinationDetail2 ->开始时间', starTime)
      // wx.showLoading({
      //   title: '加载中',
      //   mask: true,
      // });
      api.productVaccinationDetail2({
        data: {
          productId: this.data.detailData.productId
        },
        success: res => {
          // wx.hideLoading();
          const endTime = new Date().getTime()
          wx.reportPerformance(1001, endTime - starTime, 'SUCCESS')
          console.log('productVaccinationDetail2 ->结束时间', endTime)
          console.log('productVaccinationDetail2 => 时间差', endTime - starTime)
          if (res.code * 1 === 0) {
            this.setData({
              mirrorInfos: res.data.mirrorInfos,
              dialogShow: true
            })
          }
        },
        fail: err => {
          // wx.hideLoading();
          const endTime = new Date().getTime()
          wx.reportPerformance(1001, endTime - starTime, 'FAIL')
          console.log('productVaccinationDetail2 ->结束时间', endTime)
          console.log('productVaccinationDetail2 => 时间差', endTime - starTime)
        }
      })
    } else {
      this.setData({
        mirrorInfos: this.data.detailData.mirrorInfos,
        dialogShow: true
      })
    }

  },

  cancelDiaglog() {
    this.setData({
      dialogShow: false
    })
  },

  getList() {
    const that = this;
    if (that.data.type === 1) {
      api.productVaccinationDetail({ // 调用接口，传入参数
        data: {
          productMirrorId: this.data.productMirrorId * 1
        },
        success: res => {
          if (res.code * 1 === 0) {
            let paramInfosNum = 0
            res.data.paramInfos.forEach(item => {
              if (item.title === '包含针数') {
                paramInfosNum = item.value
              }
            })
            that.setData({
              detailData: res.data,
              paramInfosNum: paramInfosNum
            })
            const article = res.data.descriptionImage.replace('<style>img{width:100%}</style>', '');
            WxParse.wxParse('article', 'html', article, that, 0);
          }
        },
        fail: err => {
          console.log(err)
        }
      })
    } else {
      api.productVaccinationDetail2({
        data: {
          productId: that.data.productId
        },
        success: res => {
          if (res.code * 1 !== 0) {
            wx.showToast({
              title: res.msg,
              icon: 'error',
              duration: 2000,
              complete: function () {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
            return
          }
          let paramInfosNum = 0
          res.data.paramInfos.forEach(item => {
            if (item.title === '包含针数') {
              paramInfosNum = item.value
            }
          })
          if (res.code * 1 === 0) {
            that.setData({
              detailData: res.data,
              paramInfosNum: paramInfosNum
            })
            const article = res.data.descriptionImage.replace('<style>img{width:100%}</style>', '');
            WxParse.wxParse('article', 'html', article, that, 0);
          }
        }
      })
    }
  },

  moveToCustomer() {
    if (!wx.getStorageSync('token') || !wx.getStorageSync('bindMobile')) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../login/login`
        })
      }, 2000)
      return
    }
    customerApi.messageCompany({
      data: {},
      success: res => {
        if (res.code * 1 === 0) {
          app.globalData.customerChoose = res.data.conversation
          throttle(function () {
            wx.navigateTo({
              url: `../customerIntelligent/index`
            })
          }, 2000)
        }
      }
    })
  },

  showShareBox() {
    this.setData({
      showShare: true
    })
  },

  closeShare() {
    this.setData({
      showShare: false
    })
  },

  shareMes(e) {
    let type = e.detail.type
    if (type === 'playbill') {
      console.log(123)
    }
  },
  closeMessage() {
    this.setData({
      showWaringMessage: false
    })
  },
  showContet() {
    this.setData({
      showWaringMessage: true,
      chooseTypeItem: this.data.detailData.vaccineCategoryInfo
    })
  },
  alreadyRead() {
    wx.showToast({
      title: '订阅成功',
      type: 'success'
    })
    this.setData({
      needRead: false
    })
  },
  needToRead() {
    this.setData({
      needRead: true
    })
  }
})